<?php
    namespace cash\Controller;
    use Think\Controller;
    class RechargeController extends ApibaseController
    {
        public function index()
        {
            $page = I('get.page', 1);
            $perpage = 10;
            $url = [];
            $list = M('UserRecharge');
            if (trim(I('get.user_name'))) {
                $list = $list->where(['user_name' => trim(I('get.user_name'))]);
                $url['user_name'] = trim(I('get.user_name'));
            }
            if (trim(I('get.user_mobile'))) {
                $list = $list->where(['user_mobile' => trim(I('get.user_mobile'))]);
                $url['user_mobile'] = trim(I('get.user_mobile'));
            }
            if (in_array(I('get.score_type'), [1, 2, 3, 4, 5, 6])) {
                $list = $list->where(['score_type' => trim(I('get.score_type'))]);
                $url['score_type'] = trim(I('get.score_type'));
            }
            if (in_array(I('get.is_check'), ['-1','0','1'])) {
                $list = $list->where(['is_check' => I('get.is_check')]);
                $url['is_check'] = trim(I('get.is_check'));
            }
            $getObj = clone $list;
            $getData = $list->order(['add_at' => 'desc'])->page($page, $perpage)->select();
            $this->assign('page', setPage($getObj->count(), $perpage, $page, '?' . http_build_query($url)));
            $this->assign('getData', $getData);
            $this->display('recharge/list');
        }

        /**
         * 充值审核
         * create by: liuqi
         * Date:2018-1-9 16:10:02
         */
        public function checkRecharge()
        {

            if (I('post.dosubmit')) {
                if (I('post.recharge_id')) {
                    $chargeModel = M('UserRecharge');
                    $userModel = M('UserInfo');
                    $scoreDetail = M('ScoreDetail');
                    $chargeModel->startTrans();
                    $userModel->startTrans();
                    $scoreDetail->startTrans();
                    $chargeInfo = $chargeModel->lock(true)->where(['recharge_id' => I('post.recharge_id'),'is_check'=>0,'check_at'=>0])->field('user_id,score_type,recharge_money')->find();
                    if ($chargeInfo) {
                        $set = $chargeModel->lock(true)->where(['recharge_id' => I('post.recharge_id'),'is_check'=>0,'check_at'=>0])->setField(['is_check' => 1, 'operator' => session('loginName'), 'remark' => trim(I('post.remark')), 'update_at' => time(), 'check_at' => time()]);
                        if ($set) {
                            switch ($chargeInfo['score_type']) {
                                case '1':
                                    $score_type = 'ordinary_score';
                                    break;
                                case '2':
                                    $score_type = 'platina_score';
                                    break;
                                case '3':
                                    $score_type = 'diamonds_score';
                                    break;
                                case '4':
                                    $score_type = 'experience_score';
                                    break;
                                case '5':
                                    $score_type = 'gold_score';
                                    break;
                                case '6':
                                    $score_type = 'mouth_score';
                                    break;
                                default:
                                    break;
                            }
                            $update = $userModel->lock(true)->where(['user_id' => $chargeInfo['user_id']])->setInc($score_type, $chargeInfo['recharge_money']);
                            if ($update) {
                                $inset = $scoreDetail->lock(true)->add(['user_id' => $chargeInfo['user_id'], 'detail_type' => 1, 'detail_score' => $chargeInfo['recharge_money'], 'score_type' => $chargeInfo['score_type'], 'remark' => '充值', 'add_at' => time(), 'update_at' => time()]);
                                if ($inset) {
                                    if($chargeInfo['score_type'] == 1){
                                        //充值的积分为普通积分，增加相应的体验积分
                                        $exp = $userModel->lock(true)->where(['user_id' => $chargeInfo['user_id']])->setInc('experience_score', $chargeInfo['recharge_money']);
                                        if($exp){
                                            $ordinaryDetail = $scoreDetail->lock(true)->add(['user_id' => $chargeInfo['user_id'], 'detail_type' => 1, 'detail_score' => $chargeInfo['recharge_money'], 'score_type' => 4, 'remark' => '充值', 'add_at' => time(), 'update_at' => time()]);
                                            if($ordinaryDetail){
                                                $scoreDetail->commit();
                                                $chargeModel->commit();
                                                $userModel->commit();
                                                $this->showtips('审核通过');
                                            }else{
                                                $chargeModel->rollback();
                                                $scoreDetail->rollback();
                                                $userModel->rollback();
                                                $this->showtips('审核失败[-2]');
                                            }
                                        }else{
                                            $chargeModel->rollback();
                                            $scoreDetail->rollback();
                                            $userModel->rollback();
                                            $this->showtips('审核失败[-1]');
                                        }
                                    }
                                    $scoreDetail->commit();
                                    $chargeModel->commit();
                                    $userModel->commit();
                                    $this->showtips('审核通过');
                                } else {
                                    $chargeModel->rollback();
                                    $scoreDetail->rollback();
                                    $userModel->rollback();
                                    $this->showtips('审核失败[-3]');
                                }
                            } else {
                                $userModel->rollback();
                                $chargeModel->rollback();
                                $this->showtips('审核失败[0]');
                            }
                        } else {
                            $this->showtips('审核失败[1]');
                        }
                    } else {
                        $this->showtips('审核失败[2]');
                    }
                }
            }

            if (I('get.recharge_id')) {
                $this->assign('recharge_id', I('get.recharge_id'));
                $this->display('recharge/check_recharge');
            }
        }

        /**
         * 取消审核
         */
        public function delRecharge()
        {
            if(I('post.dosubmit')){
                $info = M('UserRecharge')->where(['recharge_id'=>I('post.recharge_id')])->find();
                if($info['is_check'] == '0'){
                    $del = M('UserRecharge')->where(['recharge_id'=>I('post.recharge_id')])->setField(['is_check'=>-1,'remark'=>trim(I('post.remark')),'update_at'=>time(),'check_at'=>time()]);
                    if($del){
                        $this->showtips('取消成功');
                    }
                }
            }
            if(I('get.recharge_id')){
                $this->assign('recharge_id', I('get.recharge_id'));
                $this->display('recharge/del_recharge');
            }
        }

        /**
         * 统计已审核的金额
         * create by: liuqi
         * Date:2018-1-9 10:08:34
         */
        public function tongji()
        {
            $data = M('UserRecharge')->where(['is_check'=>1])->sum('recharge_money');
            $data = $data?$data:0;
            echo json_encode(['code'=>0,'count'=>$data,'msg'=>'success']);
        }
    }